package com.cys.dm.rest.service;

import com.cys.dm.client.order.DmOrderClient;
import com.cys.dm.client.pay.DmTradeClient;
import com.cys.dm.client.scheduler.DmSchedulerSeatClient;
import com.cys.dm.entity.order.DmOrder;
import com.cys.dm.entity.pay.DmTrade;
import com.cys.dm.entity.scheduler.DmSchedulerSeat;
import com.cys.dm.vo.pay.ConfirmPayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author blue_t
 * @date 2020-12-28 10:08
 */
@Service
public class DmpayServier {
    @Autowired
    private DmOrderClient dmOrderClient;
    @Autowired
    private DmSchedulerSeatClient dmSchedulerSeatClient;
    @Autowired
    private DmTradeClient dmTradeClient;

    /**
     * 根据订单号查询订单详情
     * @param map
     * @return
     */
    public ConfirmPayVo findByOrderNo(Map<String,String> map){
        //返回数据结构
        ConfirmPayVo orderVo = new ConfirmPayVo();
        //订单编号
        String orderNo = map.get("orderNo");
        //订单信息
        DmOrder dmOrder = dmOrderClient.findOrderByOrderNo(orderNo);
        //座位信息
        List<DmSchedulerSeat> seatList = dmSchedulerSeatClient.findSchedulerSeatByOrderNo(orderNo);
        StringBuffer x = new StringBuffer();
        seatList.forEach(dmSchedulerSeat -> {
            x.append(dmSchedulerSeat.getX());
            x.append("_");
            x.append(dmSchedulerSeat.getY());
            x.append(",");
        });

        //订单编号
        orderVo.setOrderNo(orderNo);
        //商品名称
        orderVo.setItemName(dmOrder.getItemName());
        //座位信息
        orderVo.setSeatNames(x.toString());
        //座位数量
        orderVo.setSeatCount(x.toString().split(",").length);
        //订单金额
        orderVo.setTotalAmount(dmOrder.getTotalAmount());

        return orderVo;
    }

    public Integer addTrade(String orderNo,String tradeNo,String price){
        DmTrade dmTrade = new DmTrade();
        dmTrade.setOrderNo(orderNo);
        dmTrade.setTradeNo(tradeNo);
        dmTrade.setPayMethod(1);
        dmTrade.setAmount(Double.parseDouble(price));
        return dmTradeClient.addTrade(dmTrade);
    }
}
